<template>
  <a-modal
    :title="model && model.id ? '编辑收款方式' : '新建收款方式'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    okText=""
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item label="id" v-show="false">
          <a-input v-decorator="['id', { initialValue: model?.id }]" />
        </a-form-item>
        <a-form-item label="收款方式名">
          <a-input
            v-decorator="[
              'name',
              { initialValue: model?.name, rules: [{ required: true, message: '请输入收款方式名' }] },
            ]"
            placeholder="请输入收款方式名"
          />
        </a-form-item>
        <a-form-item label="分类">
          <a-select v-decorator="['paymentTypeId']" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in this.classOption" :key="i.value">{{ i.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="费率">
          <a-tooltip>
            <template #title>填0表示无费率</template>
            <a-input v-decorator="['rate', { initialValue: 0 }]" addon-after="%" placeholder="请输入费率" />
          </a-tooltip>
        </a-form-item>
        <a-form-item label="封顶">
          <a-tooltip>
            <template #title>填0表示不封顶</template>
            <a-input v-decorator="['top', { initialValue: 0 }]" addon-after="元" placeholder="请输入封顶" />
          </a-tooltip>
        </a-form-item>
        <a-form-item label="到账账户">
          <a-select
            v-decorator="[
              'accountId',
              {
                rules: [{ required: true, message: '请选择到账账户' }],
              },
            ]"
            placeholder="请选择"
          >
            <a-select-option :value="i.id" v-for="i in this.accOption" :key="i.value">{{ i.accName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="状态" v-show="model && model.id > 0">
          <a-select v-decorator="['status', { initialValue: model?.status }]" placeholder="请选择">
            <a-select-option value="1">使用中</a-select-option>
            <a-select-option value="2">停用</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getStAccountListUsingGET } from '@/api/api/qiantaizhangwu.ts'
import { getPaymentTypeListUsingPOST } from '@/api/api/xitongguanli.ts'
// 表单字段
const fields = ['name', 'paymentTypeId', 'rate', 'top', 'accountId', 'status', 'id']
export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      accOption: [],
      classOption: [],
      optionParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
        classType: 1,
      },
      form: this.$form.createForm(this),
    }
  },
  mounted() {
    this.handle()
    this.handleSearch()
  },
  methods: {
    handleSearch() {
      getPaymentTypeListUsingPOST({
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      })
        .then((res) => {
          this.classOption = res.data.list
        })
        .catch((error) => {
          this.loading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handle() {
      getStAccountListUsingGET(this.queryParam)
        .then((res) => {
          this.accOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
  created() {
    console.log('custom modal created')
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
}
</script>
